/** * 解法一:以数组中元素排序为准 */ public List<List<Integer>> permute(int[] nums) { if (nums == null || nums.length == 0) { returnnew ArrayList<>(); } List<List<Integer>> result = new ArrayList<>(); permute(nums, 0, result); return result; }
privatevoidpermute(int[] nums, int i, List<List<Integer>> result){ if (nums.length == i) { List<Integer> cur = new ArrayList<>(); for (Integer integer : nums) { cur.add(integer); } result.add(cur); return; } for (int j = i; j < nums.length; j++) { swap(nums, i, j); permute(nums, i + 1, result); swap(nums, i, j); } }
privatevoidswap(int[] nums, int m, int n){ int temp = nums[m]; nums[m] = nums[n]; nums[n] = temp; }
privatevoidhandle(char[] chars, int i, String cur, List<String> result){ if (i == chars.length) { result.add(cur); return; } if (Character.isDigit(chars[i])) { handle(chars, i + 1, cur + chars[i], result); } if (Character.isLowerCase(chars[i])) { handle(chars, i + 1, cur + chars[i], result); handle(chars, i + 1, cur + Character.toUpperCase(chars[i]), result); } if (Character.isUpperCase(chars[i])) { handle(chars, i + 1, cur + chars[i], result); handle(chars, i + 1, cur + Character.toLowerCase(chars[i]), result); } }